/**
 * 判断是否是外部链接
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * 判断是否是有效的URL
 * @param {string} url
 * @returns {Boolean}
 */
export function isValidURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * 判断是否是有效的邮箱
 * @param {string} email
 * @returns {Boolean}
 */
export function isValidEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * 判断是否是有效的手机号
 * @param {string} phone
 * @returns {Boolean}
 */
export function isValidPhone(phone) {
  const reg = /^1[3-9]\d{9}$/
  return reg.test(phone)
}

/**
 * 判断是否是有效的身份证号
 * @param {string} idCard
 * @returns {Boolean}
 */
export function isValidIdCard(idCard) {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return reg.test(idCard)
}

/**
 * 判断是否是JSON字符串
 * @param {string} str
 * @returns {Boolean}
 */
export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      const obj = JSON.parse(str)
      return typeof obj === 'object' && obj !== null
    } catch (e) {
      return false
    }
  }
      return false  }// 验证手机号export function isValidPhone(phone) {  return /^1[3-9]\d{9}$/.test(phone)}// 验证邮箱export function isValidEmail(email) {  return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email)}// 验证非空
export function isNotEmpty(value) {
  return value !== undefined && value !== null && value !== ''
}

// 验证身份证号
export function isValidIdNumber(idNumber) {
  return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idNumber)
}

// 验证密码(至少包含字母和数字的6-20位字符)
export function isValidPassword(password) {
  return /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,20}$/.test(password)
}

// 验证URL
export function isValidUrl(url) {
  return /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w.-]*)*\/?$/.test(url)
}

// 验证正整数
export function isPositiveInteger(value) {
  return /^[1-9]\d*$/.test(value)
}

// 验证金额(最多两位小数)
export function isValidAmount(value) {
  return /^\d+(\.\d{1,2})?$/.test(value)
}

// Element Plus表单验证规则
export const formRules = {
  // 必填项验证
  required: {
    required: true,
    message: '此项不能为空',
    trigger: ['blur', 'change']
  },
  
  // 手机号验证
  phone: {
    validator: (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!isValidPhone(value)) {
        callback(new Error('请输入有效的手机号码'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  },
  
  // 邮箱验证
  email: {
    validator: (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!isValidEmail(value)) {
        callback(new Error('请输入有效的邮箱地址'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  },
  
  // 身份证号验证
  idNumber: {
    validator: (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!isValidIdNumber(value)) {
        callback(new Error('请输入有效的身份证号码'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  },
  
  // 密码验证
  password: {
    validator: (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!isValidPassword(value)) {
        callback(new Error('密码必须包含字母和数字，长度为6-20位'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  },
  
  // 确认密码验证
  confirmPassword: (passwordField) => ({
    validator: (rule, value, callback) => {
      const form = rule.form
      if (value !== form[passwordField]) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  }),
  
  // 金额验证
  amount: {
    validator: (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!isValidAmount(value)) {
        callback(new Error('请输入有效的金额，最多两位小数'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  }
}

export default {
  isNotEmpty,
  isValidPhone,
  isValidEmail,
  isValidIdNumber,
  isValidPassword,
  isValidUrl,
  isPositiveInteger,
  isValidAmount,
  formRules
} 